# -*- coding:utf-8 -*-

#depreated 已经被废弃

import unittest
import numpy

from wolfox.foxit.dune.dyn_service import *
from wolfox.foxit.dune.models import XDayQuote

from wolfox.foxit.dune.test_common import prepare,clean


class DynDataTest(unittest.TestCase):
    
    def setUp(self):
        prepare()

    def tearDown(self):
        clean()
    
    def test_init(self):
        ds1 = DynData(XDayQuote,excluded_fields=('id','tstock'),order_fields=('tdate',),mandatory_fields=('tdate',),condition_functor=lambda stock_id,begin,end : ' tstock_id = %d and tdate>=%d and tdate<=%d' % (stock_id,begin,end))
        self.assertEquals('dune_xdayquote',ds1.table_name)
        self.assertEquals(set(['topen','tclose','thigh','tlow','tdate','tamount','tvolume','tavg']),ds1.fields)

    def test_set_required_fields(self):
        ds1 = DynData(XDayQuote,excluded_fields=('id','tstock'),order_fields=('tdate',),mandatory_fields=('tdate',),condition_functor=lambda stock_id,begin,end : ' tstock_id = %d and tdate>=%d and tdate<=%d' % (stock_id,begin,end))
        ds1.set_required_fields('topen','tclose')
        self.assertEquals(set(['topen','tclose','tdate']),ds1.required_fields)
        self.assertRaises(AssertionError,ds1.set_required_fields,'tstock')  #字段不在允许范围内
        self.assertRaises(AssertionError,ds1.set_required_fields,'id')  #字段不在允许范围内
        self.assertRaises(AssertionError,ds1.set_required_fields,'t_none')  #字段非法

    def test_records2arrays(self):
        ds1 = DynData(XDayQuote,excluded_fields=('id','tstock'),order_fields=('tdate',),mandatory_fields=('tdate',),condition_functor=lambda stock_id,begin,end : ' tstock_id = %d and tdate>=%d and tdate<=%d' % (stock_id,begin,end))
        ds1.set_required_fields('topen','tclose')
        #print ds1.required_fields
        records = [(100,20010101,101),(110,20010102,111),(120,20010103,121)]
        rev = ds1.records2arrays(records)
        #先验证数据类型
        self.assertEquals(numpy.ndarray,type(rev['topen']))
        self.assertEquals(numpy.ndarray,type(rev['tclose']))
        self.assertEquals(numpy.ndarray,type(rev['tdate']))        
        #再验证数据
        self.assertEquals([100,110,120],list(rev['topen']))
        self.assertEquals([101,111,121],list(rev['tclose']))    
        self.assertEquals([20010101,20010102,20010103],list(rev['tdate']))        

    def test_records2arrays_zerosize(self):
        ds1 = DynData(XDayQuote,excluded_fields=('id','tstock'),order_fields=('tdate',),mandatory_fields=('tdate',),condition_functor=lambda stock_id,begin,end : ' tstock_id = %d and tdate>=%d and tdate<=%d' % (stock_id,begin,end))
        ds1.set_required_fields('topen','tclose')
        #验证size为0的情况
        records = []
        rev = ds1.records2arrays(records)
        #先验证数据类型
        self.assertEquals(numpy.ndarray,type(rev['topen']))
        self.assertEquals(numpy.ndarray,type(rev['tclose']))
        self.assertEquals(numpy.ndarray,type(rev['tdate']))        
        #再验证数据
        self.assertEquals([],list(rev['topen']))
        self.assertEquals([],list(rev['tclose']))    
        self.assertEquals([],list(rev['tdate']))        
        

    def test_get_arrays(self):
        ds1 = DynData(XDayQuote,excluded_fields=('id','tstock'),order_fields=('tdate',),mandatory_fields=('tdate',),condition_functor=lambda stock_id,begin,end : ' tstock_id = %d and tdate>=%d and tdate<=%d' % (stock_id,begin,end))
        ds1.set_required_fields('topen','tclose')
        rev = ds1.get_arrays('SH00000X',0,99999999)
        #先验证数据类型, 因为是真实测试，所以尽管records2arrays中验证了(为虚拟测试)，这里仍然验证为上
        self.assertEquals(numpy.ndarray,type(rev['topen']))
        self.assertEquals(numpy.ndarray,type(rev['tclose']))
        self.assertEquals(numpy.ndarray,type(rev['tdate']))        
        #再验证数据
        self.assertEquals([12000,0],list(rev['topen']))
        self.assertEquals([12500,0],list(rev['tclose']))        
        self.assertEquals([20070705, 20080705],list(rev['tdate']))


class BasicDataTest(unittest.TestCase):
    def setUp(self):
        prepare()

    def tearDown(self):
        clean()

    def test_init(self):
        b1 = BasicData(StockCode,('id','total_size','outstanding_size'),u" exchange_id in ('上海证券交易所','深圳证券交易所')")        
        self.assertEquals('dune_stockcode',b1.table_name)

    def test_get_records(self):
        b1 = BasicData(StockCode,('id','total_size','outstanding_size')," exchange_id in ('SHSE','SZSE')")
        records = b1.get_records()
        self.assertTrue(len(records)>0)
        self.assertTrue('SH00000X' in records)
        self.assertTrue('SH00TEST' not in records)
        self.assertEquals((0,0),records['SH00000X'][1:])

class ModuleTest(unittest.TestCase):
    def setUp(self):
        prepare()

    def tearDown(self):
        clean()
    
    def test_daysource(self):
        for s in dyn_source:    #通透性测试
            s.set_required_fields() #确认默认情况下字段之间的包含关系
            s.get_arrays('SH00000X')

    def test_basicsource(self):
        for s in basic_source:    #通透性测试
            s.get_records()


